package com.android.music.prolyric;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONObject;

import com.android.music.prolyric.database.DatabaseManger;
import com.android.music.prolyric.search.SearchLyric;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LyricService extends Service
{

	static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";   
    private static final  String urlIndex = "http://loidich.com/index.php";
    DatabaseManger data ;
    SearchLyric search;
	String songname;
	String singer;
	String urlSong;
	int lyricID;	
	int localSearch = 0 ;
	String singerSearch = "";
	DoBackGround dobgr;
    
	@Override
	public void onCreate() 
	{
		super.onCreate();
		data = new DatabaseManger(getBaseContext());
	}

	@Override
	public IBinder onBind(Intent intent) 
	{
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) 
	{
		search = new SearchLyric();
		songname = intent.getExtras().getString("songname");
		singer = intent.getExtras().getString("singer");
		urlSong = intent.getExtras().getString("urlSong");
		lyricID = intent.getExtras().getInt("lyricID");
		Log.d("LoadLyricService_lyricID", lyricID+"");
		
		
		if(singer!=null && singer.indexOf("unknow")>0)
			singerSearch = null;
		else 
			singerSearch = singer;
		
		dobgr = new DoBackGround();
		dobgr.execute("");
		
		
		return START_STICKY;
	}

	@Override
	public void onDestroy() 
	{
		if(!dobgr.isCancelled())
			dobgr.cancel(true);
		if(dobgr.isCancelled())
			Log.i("Finsh", "ok");
		super.onDestroy();
	}	
	
	private class DoBackGround extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>>
	{
		
		@Override
		protected void onCancelled() 
		{
			super.onCancelled();
		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... params) 
		{
			ArrayList<HashMap<String, String>> lyricResult = null;
			Log.i("songname", songname==null?"null":songname);
			Log.i("singer", singer==null?"null":singer);
			Log.i("urlSong", urlSong==null?"null":urlSong);
			if(urlSong != null)
			{
				if(songname!= null)
				{
					
					lyricResult = data.getLyricByName(songname, singer);
					if(lyricResult == null)
					{
						localSearch = 1;
						lyricResult = data.getLyricFromHost(songname, singerSearch);
						if(lyricResult == null)
						{
							Log.d("tim tren web ", "tim tren web");
							localSearch = 2; 
							try {
								lyricResult = search.getLyricFromURL(urlSong);
								Log.i("hello", "hello");
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
						
					}
					
				}
				else if(songname==null&&singer==null)
				{
					Log.i("tim theo url ", "Chi tim theo url ");
					localSearch = 1; 
					try {
						lyricResult = search.getLyricFromURL(urlSong);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				
				
			
			}
			else if(songname != null)
			{
				if(singer!=null)
				lyricResult = data.getLyricByName(songname, singer);
				if(lyricResult == null)
				{
					localSearch = 1;
					lyricResult = data.getLyricFromHost(songname, singerSearch);
					if(lyricResult == null)
					{
						Log.d("tim tren web ", "tim tren web");
						localSearch = 2; 
						try {
							lyricResult =search.getLyricByName(songname, singerSearch);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
			return lyricResult;
		}

		@Override
		protected void onPostExecute(ArrayList<HashMap<String, String>> lyricResult) 
		{
			//gui du lieu ve
			Intent broadcastIntent = new Intent();
			broadcastIntent.setAction("lOAD_LYRIC_FROM_WEB");
			Bundle b = new Bundle();
			b.putSerializable("lyricLoader", lyricResult);
			b.putInt("lyricID", lyricID);
			b.putInt("localSearch", localSearch);
			broadcastIntent.putExtras(b);
			getBaseContext().sendBroadcast(broadcastIntent);
			stopSelf();
		}
		
		
	}

}
